'use strict'

// $(function() {
    angular.module("NetworkRequest", [])
        .config(function ($httpProvider) {
            // $httpProvider.defaults.withCredentials = true;//允许发送cookie
            // $httpProvider.defaults.headers.common['Authorization'] = document.cookie.split(";")[0].split("user_token=")[1];
        })
        .controller("NetworkRequestCtro", ['$scope', '$http', function ($scope, $http) {
            // 读取cookie eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEifQ==.838a31f45466dbdcb4b8f0ed14322bb4df392fd0365126af73d4cc13680035b6
            $scope.user_token = document.cookie.split(";")[0].split("user_token=")[1]
            console.log( $scope.user_token)
            if($scope.user_token !== ''){
                console.log(document.cookie)
                $scope.loginStatus = "已登录"
                console.log($scope.loginStatus)
            }else{
                $scope.loginStatus = "未登录"
                console.log($scope.loginStatus)
                window.history.go(-1)
            }
            //退出登录
            $scope.logoutClick = function () {
                document.cookie="user_token="+"";
                $scope.user_token = ""
                console.log( document.cookie)
                window.location.href = $scope.router.tologin
                // $cookieStore.remove(user_token)
            }



            // 页面跳转
            $scope.router = {
                // 首页面
                tologin:'./login.html',
            }

           // 请求url
            $scope.url = {
                base: "http://120.78.92.190/CommunityUnion/public",
                // base:"http://testdomain.xiaokuango.com",
                login: "/v2/auth/manager/get_token",//管理员登录
                organizeList: "/v2/manager/community/list/all",//获取组织列表
                verifyOrganize: "/v2/manager/community/verify",//获取视频详情


            }



            //注意$http get 拼接参数用params   post用data
            $scope.GET = function (url, paramter, success, error) {

                $http({
                    method: 'GET',
                    url: $scope.url.base + url,
                   // url: url,
                   //  xhrFields: {
                   //      withCredentials: true
                   //   },
                    // contentType:false,
                    // processData:false,
                    cache:true,
                    crossDomain:true,
                    params: paramter,
                    headers: {
                        // 'Access-Control-Allow-Origin':true,
                        'Content-Type': 'application/json',
                        'Authorization': $scope.user_token//"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEifQ==.838a31f45466dbdcb4b8f0ed14322bb4df392fd0365126af73d4cc13680035b6"
                    },
                }).success(
                    function (res) {
                        success(res)
                    },
                ).error(
                    function (error) {
                        error(error)
                    }
                )
            }

            $scope.POST = function (url, paramter, success, error) {
                console.log(url, paramter)
                $http({
                    method: 'POST',
                    url: $scope.url.base + url,
                    data: paramter,
                    header: {
                        'content-type': 'application/json',
                        Authorization: $scope.user_token
                    },
                }).success(
                    function (res) {
                        success(res)
                    },
                ).error(
                    function (error) {
                        error(error)
                    }
                )
            }
            $scope.ajaxForm = function (url, formData, success, fail) {
                console.log(url,formData)
                $.ajax({
                    url: $scope.url.base + url,
                    data: formData,
                    type: 'POST',
                    crossDomain:true,
                    contentType: false,
                    processData: false,
                    headers: {
                        'Authorization': $scope.user_token//'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEifQ==.838a31f45466dbdcb4b8f0ed14322bb4df392fd0365126af73d4cc13680035b6'
                    },
                    success: function (res) {
                        success(res)

                    },
                    error: function (err) {
                        fail(err)
                    }
                })

            }
            //获取社团列表
            $scope.organizeList = function (college_id, page, count, success, fail) {
                var parameter = {
                    "college_id":college_id,
                    "page": page,
                    "count": count,
                    // "jsonp":"JSON_CALLBACK"
                }
                // console.log(parameter, success, fail)
                $scope.GET($scope.url.organizeList, parameter, success, fail)
            }
            //审核社团
            $scope.verifyOrganize = function ( community_id, oper, success, fail) {
                var parameter = {
                    "community_id":community_id,
                    "oper":oper,
                }
                console.log( parameter, success, fail)
                $scope.GET($scope.url.verifyOrganize,parameter, success, fail)
            }


        }]);


// })